<template>
   
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="searchTagList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.keyword"
              placeholder="标签名称"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        style="margin: 0; margin-left: 20px; padding: 3px 0"
        type="text"
        @click="handleBatchImport"
        >从文件中批量导入</el-button
      >
      <el-button class="btn-add" @click="handleAdd()" size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="brandTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="标签名称" align="center">
          <template slot-scope="scope"
            ><b>{{ scope.row.name }}</b></template
          >
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="创建人" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.creatorName }}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.ctime }}</template>
        </el-table-column>
        <el-table-column label="修改人" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.updateName }}</template>
        </el-table-column>
        <el-table-column label="修改时间" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.mtime }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.$index, scope.row)"
              >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchDelete()"
        type="primary"
        size="small"
      >
        批量删除
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 20, 50, 200]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加标签"
      :visible.sync="dialogVisible"
      width="30%"
      center
    >
      <el-form :model="tag" ref="tagForm" label-width="150px" size="middle">
        <el-form-item label="标签名称：">
          <el-input v-model="tag.name" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input v-model="tag.sort" style="width: 250px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="批量导入敏感词"
      :visible.sync="fileDialogVisible"
      width="30%"
      center
    >
      <div class="upload-container">
        <el-upload
          ref="upload"
          action="none"
          :file-list="fileList"
          :auto-upload="false"
          accept="text/plain"
          multiple
          :limit="3"
        >
          <el-button
            slot="trigger"
            size="small"
            type="primary"
            style="width: 100px"
            >选取文件</el-button
          >
          <div class="el-upload__tip" slot="tip" align="center">
            <p>只能上传txt文件，且不超过500KB，最多可选择三个文件</p>
            <h4 style="margin: 0">
              请注意文件格式应该保证：<b>单个词语占据一行</b>
            </h4>
            <h4 style="margin: 0">部分已存在的标签在导入时会自动跳过</h4>
          </div>
        </el-upload>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="fileDialogVisible = false" size="small"
          >取 消</el-button
        >
        <el-button
          type="success"
          @click="submitUpload"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchList,
  deleteTag,
  batchDelete,
  updateTag,
  createTag,
  batchImportFromFile,
} from "@/api/tag";
const defaultTag = {
  id: null,
  name: null,
  sort: 0,
};
export default {
  name: "tagList",
  data() {
    return {
      dialogVisible: false,
      listQuery: {
        keyword: null,
        pageNum: 1,
        pageSize: 10,
      },
      list: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      tag: Object.assign({}, defaultTag),
      isEdit: false,
      fileDialogVisible: false,
      fileList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleBatchImport() {
      this.fileDialogVisible = true;
    },
    submitUpload() {
      let formData = new FormData();
      let uploads = this.$refs.upload.uploadFiles;
      uploads.forEach((file) => {
        formData.append("files", file.raw);
      });
      // let file = this.$refs.upload.uploadFiles[0].raw;
      this.fileDialogVisible = true;
      this.$message({
        message: "发起成功，后台将在一段时间内处理您的请求，请勿重复发起！",
        type: "success",
        duration: 1000,
      });
      batchImportFromFile(formData)
        .then((res) => {})
        .catch((err) => {
        });
      this.getList();
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        // console.log(response.data);
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
        this.totalPage = response.data.totalPage;
        this.pageSize = response.data.pageSize;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.isEdit = true;
      this.tag = Object.assign({}, row);
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该标签", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteTag(row.id).then((response) => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000,
          });
          this.getList();
        });
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    searchTagList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleBatchDelete() {
      // console.log(this.multipleSelection);
      if (this.multipleSelection < 1) {
        this.$message({
          message: "请选择一条记录",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      this.$confirm("是否要进行该批量操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        let data = new URLSearchParams();
        data.append("ids", ids);
        batchDelete(data).then((response) => {
          this.getList();
          this.$message({
            message: "修改成功",
            type: "success",
            duration: 1000,
          });
        });
      });
    },
    handleAdd() {
      this.dialogVisible = true;
      this.isEdit = false;
      this.tag = Object.assign({}, defaultTag);
    },
    handleDialogConfirm() {
      if (this.isEdit) {
        updateTag(this.tag.id, this.tag).then((response) => {
          this.$message({
            message: "修改成功！",
            type: "success",
          });
          this.dialogVisible = false;
          this.getList();
        });
      } else {
        createTag(this.tag).then((response) => {
          this.$message({
            message: "添加成功！",
            type: "success",
          });
          this.dialogVisible = false;
          this.getList();
        });
      }
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>